package com.toy.exception;

import com.toy.result.ResponseEnum;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * 统一异常处理器
 * @author tystart
 * @create 2022-09-01-20:36
 */
@RestControllerAdvice
public class UnifiedExceptionHandler {
    @ExceptionHandler(value = ToyMallException.class)
    public ModelAndView handlerException(ToyMallException e){
        ModelAndView modelAndView = new
                ModelAndView();
        ResponseEnum responseEnum =
                e.getResponseEnum();
        switch (responseEnum.getCode()){
            case 301:
                modelAndView.setViewName("register");
                modelAndView.addObject("emailError",
                        responseEnum.getMsg());
                break;
            case 302:
                modelAndView.setViewName("register");
                modelAndView.addObject("mobileError",
                        responseEnum.getMsg());
                break;
            case 303:
                modelAndView.setViewName("register");
                modelAndView.addObject("userNameExists",
                        responseEnum.getMsg());
                break;
            case 305:
                modelAndView.setViewName("login");
                modelAndView.addObject("userNameError",
                        responseEnum.getMsg());
                break;
            case 306:
                modelAndView.setViewName("login");
                modelAndView.addObject("passWordError",
                        responseEnum.getMsg());
                break;
            case 308:
                modelAndView.setViewName("login");
                break;
        }
        return modelAndView;
    }
}
